//
// substation.h
// substation
// Application Server Substation / Substation acts as controller/passthru on a single machine
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
// 
// Please send correspondence to:
// 
// theproteaproject@gmail.com
//

/*
	\file substation.h
	\brief Substation - Machine substation link to GCS

	Substation - One substation exists on each machine in the Traffic system, and is essentially a
		pass-through station for all messages between GCS and the Dispatchers

	Usage : substation <app> <gcs_host_ip> <gcs_listen_port> [\<pipe\>]

		app - Application name (or number NOT CURRENTLY SUPPORTED)
		gcs_host_ip - Host IP address (or host name) of Grand Central Station
		gcs_listen_port - Listener socket port of Grand Central Station
		pipe (optional) - handle to pipe for parent process (GCS)

*/
#ifndef __PROTEA_SUBSTATION_H__
#define __PROTEA_SUBSTATION_H__

// XCSubstation - Substation
class XCSubstation : public XCAppInterface, public parms_reader, public XCService
{
public:
	void ApplStop();
	void StartSubstation();
	void StartMessage();
	void StartChildApp( app_start_message* );
	void StartApps();

	void SetParms();

	char* hostname;

	// Constructor
	XCSubstation( char* name, char* dispname, char* desc, void* func, char* deps );

	// Destructor
	~XCSubstation();

protected:
	// Service functions
	void OnInit();
	void OnRun();
	void OnSignalStop();
	void OnCleanup();

	void SendGoodbye( int appnum, int pid );
	bool GotMessage( XCMsgPort* port, XCToolsMessage* msg );
	bool SubstationForwardMessage( XCMsgPort* port, XCToolsMessage* msg );

	void OnDroppedConnection( XCMsgPortPtr tempport );
	void OnDroppedProcess( pid_t process_pid );

	XCMsgPortPtr murderer;
};

#endif
